package cn.ikun.web.Servlet;

import cn.ikun.web.JdbcUtils;
import org.apache.commons.lang3.RandomStringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(urlPatterns = "/goods/save777")
@MultipartConfig(location = "E:/upload-images",maxFileSize =100*1024*1024,maxRequestSize =104857600 )
public class GoodsAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("n");
        String price = req.getParameter("p");
        Part image = req.getPart("i");
        String fileName = image.getSubmittedFileName();
        String[] strings = fileName.split("\\.");
        String suffix = strings[1];
        String img = RandomStringUtils.randomAlphabetic(6) + "." + suffix;
        image.write(img);

        String sql = "insert into goods (`name`,price,image) values(?,?,?) ";
        JdbcUtils.modify(sql,name,price,img);
        resp.sendRedirect("/ikun/goods-list.jsp");

    }
}
/*dd*/